import '../../../utils/json_converter.dart';
import 'book_package_item.dart';

class BookPackageInfo {
  final int? id;                     // ID
  final int? storeId;                // 商店ID
  final String? createUserString;       // 创建用户
  final String? createTime;             // 创建时间
  final String? updateUserString;       // 更新用户
  final String? updateTime;             // 更新时间
  final String? name;                   // 套餐名称
  final String? description;            // 套餐描述
  final double? originalTotalPrice;     // 原始总价
  final double? discountRate;           // 折扣率
  final double? salePrice;              // 销售价格
  final int? status;                    // 状态
  final String? startTime;              // 开始时间
  final String? endTime;                // 结束时间
  final bool? isDeleted;                // 是否删除
  final List<BookPackageItem>? books;   // 书籍列表

  BookPackageInfo({
    this.id,
    this.storeId,
    this.createUserString,
    this.createTime,
    this.updateUserString,
    this.updateTime,
    this.name,
    this.description,
    this.originalTotalPrice,
    this.discountRate,
    this.salePrice,
    this.status,
    this.startTime,
    this.endTime,
    this.isDeleted,
    this.books,
  });

  factory BookPackageInfo.fromJson(Map<String, dynamic> json) {
    return BookPackageInfo(
      id: JsonConverter.toInt(json['id']),
      storeId: JsonConverter.toInt(json['storeId']),
      createUserString: JsonConverter.toStr(json['createUserString']),
      createTime: JsonConverter.toStr(json['createTime']),
      updateUserString: JsonConverter.toStr(json['updateUserString']),
      updateTime: JsonConverter.toStr(json['updateTime']),
      name: JsonConverter.toStr(json['name']),
      description: JsonConverter.toStr(json['description']),
      originalTotalPrice: JsonConverter.toDouble(json['originalTotalPrice']),
      discountRate: JsonConverter.toDouble(json['discountRate']),
      salePrice: JsonConverter.toDouble(json['salePrice']),
      status: JsonConverter.toInt(json['status']),
      startTime: JsonConverter.toStr(json['startTime']),
      endTime: JsonConverter.toStr(json['endTime']),
      isDeleted: JsonConverter.toBool(json['isDeleted']),
      books: json['books'] != null
          ? List<BookPackageItem>.from(json['books'].map((i) => BookPackageItem.fromJson(i)))
          : null,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'createUserString': createUserString,
      'createTime': createTime,
      'updateUserString': updateUserString,
      'updateTime': updateTime,
      'storeId': storeId,
      'name': name,
      'description': description,
      'originalTotalPrice': originalTotalPrice,
      'discountRate': discountRate,
      'salePrice': salePrice,
      'status': status,
      'startTime': startTime,
      'endTime': endTime,
      'isDeleted': isDeleted,
      'books': books?.map((e) => e.toJson()).toList(),
    };
  }
}
